Home:ALL Converter>shell script to shutdown/restart Linux system

shell script to shutdown/restart Linux system

Ask Time:2015-06-16T17:04:50         Author:Bibin Jose

Json Formatter

Is there any suitable shell script for shutting down or restarting a Linux machine? I have tried a shell script for shutdown, but when I enter sudo shutdown it will ask for the password. How we can enter the password using the script?

Author:Bibin Jose,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30863162/shell-script-to-shutdown-restart-linux-system
bratkartoffel :

Another, in my opinion cleaner approach:\n\nCreate a new file in /etc/sudoers.d/ with content:\n\n%users ALL=NOPASSWD: /sbin/shutdown\n%users ALL=NOPASSWD: /sbin/reboot\n\n\nThis causes sudo to not ask for the password, if any user of group \"users\" tries to execute a shutdown or reboot. Of course you can also specify another group, maybe a newly created group for finer control of reboot permissions.\n\nMore information about the other possible settings for sudo can be found in the Manpage.",
2015-06-16T10:03:36
Wald :

Yes, use the -S switch which reads the password from STDIN:\n$echo <password> | sudo -S <command>\n\nSo to shut down the machine, your command would be like this (just replace <password> with your password):\n$echo <password> | sudo -S poweroff\n\nExposing your password is generally bad idea search for something that can protect / hide it. In the past I've used Jenkins plugins to do this while executing the scripts regularly.",
2015-06-16T09:08:26
yy